<?php
class EGallery extends CWidget {

	var $assets = '';
	var $options = array();
	var $model;
	var $name;
	
	/**
	 * Initializes the widget.
	 * This method is called by {@link CBaseController::createWidget}
	 * and {@link CBaseController::beginWidget} after the widget's
	 * properties have been initialized.
	 */
	public function init()
	{
		$this->assets = Yii::app()->assetManager->publish(Yii::getPathOfAlias('ext.gallery.assets'));
		$cs = Yii::app()->getClientScript();
		$cs->registerCoreScript( 'jquery' );

        $default = array(
        	'transition'=>'fade',
        	'thumb_quality'=>'auto',
        	'height'=>500,
        	'title'=>true,
        	'size'=>'normal',
        );

		$this->options = array_merge($default, $this->options);
        $options = CJavaScript::encode($this->options);
        
		if(!$this->options['manager']) {
			$cs->registerScriptFile( $this->assets.'/galleria.js' );
			$cs->registerScript(__CLASS__.'#Galleria.loadTheme',"Galleria.loadTheme('".$this->assets."/themes/lightbox/galleria.lightbox.js');");
			$cs->registerScript(__CLASS__.'#'.$this->id.'-'.$this->model->id,"$('#{$this->id}-{$this->model->id}').galleria({$options});");
		} else {
			$cs->registerCssFile( $this->assets.'/main.css' );
			$cs->registerScriptFile( $this->assets.'/egallery.jquery.js' );
			$cs->registerScript(__CLASS__.'#'.$this->id,"$('#{$this->id}').egallery({$options});");
		}
			
        parent::init();
	}

	/**
	 * Executes the widget.
	 * This method is called by {@link CBaseController::endWidget}.
	 */
	public function run()
	{
		if(isset($this->options['manager']) && $this->options['manager'])
			$this->render(__CLASS__.'Manager', array('model'=>$this->model, 'options'=>$this->options));
		else
		$this->render(__CLASS__, array('model'=>$this->model, 'options'=>$this->options));		
	}
	
	
}
?>